package homework003;

import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.plaf.synth.SynthOptionPaneUI;

public class App {
    static boolean flag =true;
	public static void main(String[] args) {
		ArrayList<Item> list = new ArrayList<Item>();
		InitFruitList(list);
		while (flag) {
			mainMenu();
			chooseFuction(list);
		}
	}

	private static void chooseFuction(ArrayList<Item> list) {
		System.out.println("请输入你要操作的功能序号:");
		Scanner sc = new Scanner(System.in);
		String choose = sc.next();
		switch (choose) {
		case "1":
			showFruitList(list);
			break;
		case "2":
			addFruitList(list);
			break;
		case "3":
			deleteFruitList(list);
			break;
		case "4":
			udpateFruitList(list);
			break;
		case "5":
			searchFruitList(list);
			break;
		case "6":
            flag=false;
			System.out.println("退出系统欢迎下次再来哦");
			return;
		default:
			break;
		}
	}

	private static void searchFruitList(ArrayList<Item> list) {
		// TODO Auto-generated method stub
		// 输入a 会出来商品 aa和ab
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入查询商品名：");
		String searchFruit = scanner.next();
		int flag = 0;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getName().contains(searchFruit)) {
				System.out.println(list.get(i).getName());
				flag = 1;
			}
		}
		if (flag == 0) {
			System.out.println("无匹配商品");
		}
	}

	private static void udpateFruitList(ArrayList<Item> list) {
		// TODO Auto-generated method stub
		// 输入商品编码 没有会给提示
		// 1.修改全部 2.修改某个字段
		Scanner scanner = new Scanner(System.in);
		System.out.println("请选择修改类型：1.修改全部\t2.修改商品数量");
		int choose = scanner.nextInt();
		switch (choose) {
		case 1:
			System.out.println("请输入需要修改的商品ID:");
			int updateflag = 0;
			int updatenumber = scanner.nextInt();
			for (int i = 0; i < list.size(); i++) {
				if (updatenumber == list.get(i).getID()) {
					System.out.println("请输入修改商品名称");
					list.get(i).setName(scanner.next());
					System.out.println("请输入修改商品价格");
					list.get(i).setPrice(scanner.nextDouble());
					System.out.println("请输入修改商品数量");
					list.get(i).setNumber(scanner.nextInt());
					updateflag = 1;
					System.out.println("ID为" + updatenumber + "的商品已修改");
					break;
				} else {
					updateflag = 0;
				}
			}
			if (updateflag == 0) {
				System.out.println("ID为" + updatenumber + "的商品不存在");
			}
			break;
		case 2:
			System.out.println("请输入需要修改的商品ID:");
			int updateflag2 = 0;
			int updatenumber2 = scanner.nextInt();
			for (int i = 0; i < list.size(); i++) {
				if (updatenumber2 == list.get(i).getID()) {
					System.out.println("已查询到对应商品信息");
					System.out.println("商品名\t商品数量");
					System.out.println(list.get(i).getName()+"\t"+list.get(i).getNumber());
					System.out.println("请输入修改商品数量");
					list.get(i).setNumber(scanner.nextInt());
					updateflag2 = 1;
					break;
				} else {
					updateflag = 0;
				}
			}
			if (updateflag2 == 0) {
				System.out.println("ID为" + updatenumber2 + "的商品不存在");
			}
			break;
		default:
			break;
		}
	}

	private static void deleteFruitList(ArrayList<Item> list) {
		// TODO Auto-generated method stub
		System.out.println("请输入需要删除的商品ID:");
		Scanner scanner = new Scanner(System.in);
		int deletenumber = scanner.nextInt();
		int deleteflag = 0;
		for (int i = 0; i < list.size(); i++) {
			if (deletenumber == list.get(i).getID()) {
				list.remove(i);
				System.out.println("ID为" + deletenumber + "的商品已删除");
				deleteflag = 1;
				break;
			} else {
				deleteflag = 0;
			}
		}
		if (deleteflag == 0) {
			System.out.println("ID为" + deletenumber + "的商品不存在");
		}
	}

	private static void addFruitList(ArrayList<Item> list) {
		// id 系统自己增加
		int maxid = list.get(0).getID();
		Scanner scanner = new Scanner(System.in);
		Item item = new Item();
		// 获取仓库商品最大id
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getID() > maxid) {
				maxid = list.get(i).getID();
			}
		}
		int goodscount;
		System.out.println("请输入需要添加的商品数量：");
		goodscount = scanner.nextInt();
		for (int i = 0; i < goodscount; i++) {
			item.setID(maxid + 1);
			System.out.println("请输入" + (i + 1) + "商品名称:");
			item.setName(scanner.next());
			System.out.println("请输入" + (i + 1) + "商品单价:");
			item.setPrice(scanner.nextDouble());
			System.out.println("请输入" + (i + 1) + "商品数量:");
			item.setNumber(scanner.nextInt());
			maxid++;
			list.add(item);
		}
	}

	private static void showFruitList(ArrayList<Item> list) {
		/*
		 * 商品编号 商品名称 商品单价 商品数量 1001 aa 10 1 1002 ab 20 2 1003 c 30 3 总价为:140 数量为6
		 */
		double totalmoney = 0;
		int count = 0;
		System.out.println("商品编号\t" + "商品名称\t" + "商品单价\t" + "商品数量");
		for (int i = 0; i < list.size(); i++) {
			System.out.print(list.get(i).getID() + "\t");
			System.out.print(list.get(i).getName() + "\t");
			System.out.print(list.get(i).getPrice() + "\t");
			System.out.print(list.get(i).getNumber());
			System.out.println();
			totalmoney += list.get(i).getPrice();
			count += list.get(i).getNumber();
		}
		System.out.println("总价为:" + totalmoney + "\t" + "数量为:" + count);
	}

	private static void mainMenu() {
		System.out.println("======欢迎来到库存管理系统============");
		System.out.println("1.查询货物2.添加新的货物3.删除货物4修改货物5.搜索商品6.退出系统");
	}

	private static ArrayList<Item> InitFruitList(ArrayList<Item> list) {
		/* 初始化商品信息 */
		Item item = new Item(1, "oppo", 2999, 1200);
		list.add(item);
		Item item1 = new Item(2, "小米", 1999, 1300);
		list.add(item1);
		Item item2 = new Item(3, "apple", 5999, 1100);
		list.add(item2);
		Item item3 = new Item(4, "华为", 6999, 1000);
		list.add(item3);
		return list;
	}
}